package com.sanjiahantang.ythshopping.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdate;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.CameraPosition;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.sanjiahantang.ythshopping.R;
import com.sanjiahantang.ythshopping.model.NearbyShop;
import com.squareup.picasso.Picasso;


public class NearbyShopDetailActivity extends Activity {

    private ImageView shopImg;
    private MapView map;
    private AMap aMap;
    private double lat, lng;

    private String url;
    private String name;

    private FrameLayout back;
    private TextView title;
    private TextView contact;
    private TextView info;
    private TextView address;
    private TextView distance;
    private NearbyShop.ShopList shopDetail;

    private CameraUpdate cameraUpdate;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nearby_detail);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        shopDetail = (NearbyShop.ShopList) bundle.get("shop");
        lat = Double.valueOf(shopDetail.getLat());
        lng = Double.valueOf(shopDetail.getLng());
       // url = ConfigValue.IMG_IP + shopDetail.getImg();
        name = shopDetail.getShop_name();

        shopImg = (ImageView) findViewById(R.id.nearby_img);
        back = (FrameLayout) findViewById(R.id.fl_Left);
        title = (TextView) findViewById(R.id.title);
        contact = (TextView) findViewById(R.id.shop_contact);
        info = (TextView) findViewById(R.id.shop_info);
        address = (TextView) findViewById(R.id.shop_address);
        distance = (TextView) findViewById(R.id.shop_distance);

        contact.setText("联系方式： " + shopDetail.getTel());
        info.setText("商家信息： " + shopDetail.getIntro());
        address.setText("商家地址： " + shopDetail.getAddress());
        distance.setText(shopDetail.getDistance());

        Picasso.with(this).load(url).into(shopImg);

        map = (MapView) findViewById(R.id.nearby_map);
        map.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = map.getMap();

        }
        cameraUpdate = CameraUpdateFactory.newCameraPosition(new CameraPosition(new LatLng(lat, lng), 18, 0, 30));
        aMap.moveCamera(cameraUpdate);
        Marker marker = aMap.addMarker(new MarkerOptions()
                .position(new LatLng(lat, lng))
                .title(name)
                .icon(BitmapDescriptorFactory
                        .fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.umeng_socialize_location_mark)))
                .draggable(true));
        marker.showInfoWindow();

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        title.setText(name);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        map.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        map.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        map.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        map.onSaveInstanceState(outState);
    }
}
